package com.L.yygh.hosp.controller;

import com.L.yygh.common.result.Result;
import com.L.yygh.hosp.service.HospitalSetService;
import com.L.yygh.model.hosp.HospitalSet;
import com.L.yygh.vo.hosp.HospitalSetQueryVo;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
import com.L.yygh.util.MD5;

//@CrossOrigin //允许跨域访问
@Controller
@RequestMapping("/admin/hosp/hospitalSet")
@RestController
@Api(tags = "医院设置管理")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //1.查询医院设置表的所有信息
    @ApiOperation("查询医院设置表的所有信息")
    @GetMapping("/findAll")
    public Result findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);

    }

    //2.删除医院设置表的相关信息
    @ApiOperation("根据ID逻辑删除医院设置表的相关信息")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable("id") Long id) {
        boolean b = hospitalSetService.removeById(id);
        return b?Result.ok(b):Result.fail(b);

    }

    //3.根据条件查询带分页
    @ApiOperation("根据条件查询带分页")
    @PostMapping("/findPage/{currentPage}/{limitPage}")
    public Result findPageHospSet(@PathVariable("currentPage") Long current,
                                  @PathVariable("limitPage") Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建分页Page对象，传递当前页和每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);

        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        //构造条件
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }

        Page<HospitalSet> page1 = hospitalSetService.page(page, wrapper);
        return Result.ok(page1);

    }

    //4.添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //1为可用，0为不可用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        String key = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(9999));
        hospitalSet.setSignKey(key);
        boolean ok = hospitalSetService.save(hospitalSet);
        return ok?Result.ok(ok):Result.fail(ok);

    }

    //5.根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @GetMapping("/getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6.修改医院设置
    @ApiOperation("修改医院设置")
    @PutMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b?Result.ok(b):Result.fail(b);
    }

    //7.批量删除医院设置
    @ApiOperation("批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean b = hospitalSetService.removeByIds(idList);
        return b?Result.ok(b):Result.fail(b);
    }

    //8.医院设置锁定和解锁
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status") Integer status){
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b?Result.ok(b):Result.fail(b);

    }

    //9.发送签名秘钥
    @ApiOperation("发送签名秘钥")
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hosname = hospitalSet.getHosname();
        String signKey = hospitalSet.getSignKey();
        //TODO 发送短信
        return Result.ok();
    }
}
